package com.wwwarehouse.taskcenter.fragment.job_point.step2.parent;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.DimensionBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_JOB_POINT_UKID;
import static com.wwwarehouse.taskcenter.fragment.job_point.step2.child.ChildJobPointDimensionListFragment.KEY_DIMENSION_DETAILS;

/**
 * Created by Jackie on 2017/10/27
 * 父作业点数量维度
 */

public class ChangeTypeParentQualityFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;

    private Button mStartAnyBtn;
    private Button mEndAnyBtn;
    private Button mEndConstantBtn;

    private Button mSaveBtn;

    private String mJobPointUkid;
    private DimensionBean mDimensionBean;

    private static final int REQUEST_SAVE_DIMENSION = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_change_type_parent_quality, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mStartAnyBtn = findView(mRootView, R.id.btn_any_start);
        mStartAnyBtn.setSelected(true);
        mEndAnyBtn = findView(mRootView, R.id.btn_any_end);
        mEndAnyBtn.setSelected(true);
        mEndConstantBtn = findView(mRootView, R.id.btn_constant_end);

        mSaveBtn = findView(mRootView, R.id.btn_save);
    }

    private void initData() {
        TaskCenterCommon.getInstance().setStart(false);
        TaskCenterCommon.getInstance().setDetails(false);
        TaskCenterCommon.getInstance().setStartMap(null);
        TaskCenterCommon.getInstance().setEndMap(null);

        Bundle bundle = getArguments();

        if (bundle != null) {
            mJobPointUkid = bundle.getString(KEY_JOB_POINT_UKID);

            mDimensionBean = (DimensionBean) bundle.getSerializable(KEY_DIMENSION_DETAILS);

            buildEchoMap(mDimensionBean);
            showEchoInfo(mDimensionBean);
        }
    }

    private void initEvent() {
        mEndAnyBtn.setOnClickListener(this);
        mEndConstantBtn.setOnClickListener(this);

        mSaveBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_any_end) {
            mEndAnyBtn.setSelected(true);
            mEndConstantBtn.setSelected(false);
        } else if (id == R.id.btn_constant_end) {
            mEndAnyBtn.setSelected(false);
            mEndConstantBtn.setSelected(true);
        } else if (id == R.id.btn_save) {
            saveQualityDimension();
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChangeTypeParentQualityFragment) {
            mActivity.setTitle(R.string.change_dimension);
        }
    }

    @Override
    public void requestDatas() {

    }

    /**
     * 保存数量维度信息
     */
    private void saveQualityDimension() {
        showProgressDialog(getString(R.string.saving));

        Map map = new HashMap();

        map.put("dimensionType", "quality");

        if (mStartAnyBtn.isSelected()) {
            Map startMap = new HashMap();
            List<Map<String, String>> startList = new ArrayList<>();
            Map<String, String> startListMap = new HashMap<>();
            startListMap.put("key", "-10002");
            startListMap.put("value", "任意");
            startList.add(startListMap);

            startMap.put("list", startList);
            startMap.put("type", "CONSTANT");
            map.put("start", startMap);
        }

        if (mEndAnyBtn.isSelected()) {
            Map endMap = new HashMap();
            List<Map<String, String>> endList = new ArrayList<>();
            Map<String, String> endListMap = new HashMap<>();
            endListMap.put("key", "-10002");
            endListMap.put("value", "任意");
            endList.add(endListMap);

            endMap.put("list", endList);
            endMap.put("type", "CONSTANT");
            map.put("end", endMap);
        } else if (mEndConstantBtn.isSelected()) {
            Map endMap = new HashMap();
            List<Map<String, String>> endList = new ArrayList<>();
            Map<String, String> endListMap = new HashMap<>();
            endListMap.put("key", "-10003");
            endListMap.put("value", "不变");
            endList.add(endListMap);

            endMap.put("list", endList);
            endMap.put("type", "CONSTANT");
            map.put("end", endMap);
        }

        map.put("jobPointUkid", mJobPointUkid);

        NoHttpUtils.httpPost(TaskCenterConstant.URL_SAVE_DIMENSION, map, mOnResponseListener, REQUEST_SAVE_DIMENSION);
    }

    /**
     * 显示回显信息
     * @param dimensionBean
     */
    private void buildEchoMap(DimensionBean dimensionBean) {
        if (dimensionBean.getStart() != null) {
            Map startMap = new HashMap();
            List<Map<String, String>> startList = new ArrayList<>();

            for (DimensionBean.StartBean.ListBean listBean : dimensionBean.getStart().getList()) {
                Map<String, String> listMap = new HashMap<>();

                listMap.put("key", listBean.getKey());
                listMap.put("value", listBean.getValue());

                startList.add(listMap);
            }

            startMap.put("list", startList);
            startMap.put("type", dimensionBean.getStart().getType());

            TaskCenterCommon.getInstance().setStartMap(startMap);
        }

        if (dimensionBean.getEnd() != null) {
            Map endMap = new HashMap();
            List<Map<String, String>> endList = new ArrayList<>();

            for (DimensionBean.EndBean.ListBean listBean : dimensionBean.getEnd().getList()) {
                Map<String, String> listMap = new HashMap<>();

                listMap.put("key", listBean.getKey());
                listMap.put("value", listBean.getValue());

                endList.add(listMap);
            }

            endMap.put("list", endList);
            endMap.put("type", dimensionBean.getEnd().getType());

            TaskCenterCommon.getInstance().setEndMap(endMap);
        }
    }

    private void showEchoInfo(DimensionBean dimensionBean) {
        if (dimensionBean.getStart() != null) {
            if ("CONSTANT".equals(dimensionBean.getStart().getType())) {

            } else {

            }
        }

        if (dimensionBean.getEnd() != null) {
            if ("CONSTANT".equals(dimensionBean.getEnd().getType())) {
                for (DimensionBean.EndBean.ListBean listBean : dimensionBean.getEnd().getList()) {
                    if ("-10002".equals(listBean.getKey())) {  //任意
                        mEndAnyBtn.setSelected(true);
                        mEndConstantBtn.setSelected(false);
                    } else if ("-10003".equals(listBean.getKey())) {   //不变
                        mEndAnyBtn.setSelected(false);
                        mEndConstantBtn.setSelected(true);
                    }
                }
            } else {

            }
        }
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_SAVE_DIMENSION:
                    if ("0".equals(commonClass.getCode())) {
                        // 保存成功, 父作业点数量维度选中
                        popFragment();

                        Intent intent = new Intent("ChangeTypeDimension");
                        mActivity.sendBroadcast(intent);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            toast(s);
        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };
}
